Skip to content

Improve numerical stability of sigmoid function - #15769

Open
mehulikhanra904-prog wants to merge 4 commits into
TheAlgorithms:masterfrom
mehulikhanra904-prog:stable-sigmoid
Open

mehulikhanra904-prog wants to merge 4 commits into
TheAlgorithms:masterfrom
mehulikhanra904-prog:stable-sigmoid

Conversation

@mehulikhanra904-prog

Copy link
Copy Markdown

Describe your change

  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?

Changes

  • Implemented numerically stable sigmoid calculations.
  • Used separate formulas for positive and negative inputs to prevent exponential overflow.
  • Added validation for non-numeric inputs.
  • Added validation for NaN and infinite values.
  • Added doctests for normal, extreme, scalar, and invalid inputs.

Checklist

  • I have read the contributing guidelines.
  • This pull request is my own work.
  • I understand that pull requests must pass automated tests.
  • This PR changes only one algorithm file.
  • Function names follow Python naming conventions.
  • Type hints are included.
  • Doctests are included.

No existing issue is being resolved.

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Nov 17, 2026
@mehulikhanra904-prog

Copy link
Copy Markdown
Author

check

Comment thread maths/sigmoid.py Resolved
Comment thread maths/sigmoid.py Resolved
Comment thread maths/sigmoid.py Resolved
@mehulikhanra904-prog

mehulikhanra904-prog commented Nov 18, 2026 via email

Copy link
Copy Markdown
Author

@mehulikhanra904-prog mehulikhanra904-prog left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out! I've removed the finite-value validation and now reject only NaN inputs. Infinite values are handled correctly by the stable branches, preserving the previous behavior. I've also added a doctest covering positive and negative infinity.

  1. Click Resolve comment.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants